var qQuery = new Query({container:$(".order_list")});
//var sSearch = new Search({ container : $(".ad_config")});
var Main = RichBase.extend({
	statics : {},
	EVENTS : {
		"click" : {
			".sure_btn" : "search_all",
			".del_btn" : "del_all",
		}},
	init : function(opt){
		var that = this;
		this.container = opt.container;
		this.prevBtn = $("#prevPageBtn");
		this.nextBtn = $("#nextPageBtn");
		// sSearch.on("search",function(opt){
			// //console.log(opt)
			// var clickData = click.getData();
			// that.prevBtn.addClass("disable");
			// $("#whichPageNum").text(1)
			// qQuery.setCurPage(0);
			// qQuery.setData();
			// qQuery.query(opt);
		// });
		
		var pid = $(".pid").val()
		var sid = $(".sid").val()
		qQuery.query({
			pid : pid,
			sid : sid
		});

	},
	search_all : function(){
		$(".fb_price").attr("qu","1");
		var pid = $(".pid").val()
		var sid = $(".sid").val()
		var txt = $("#search_text").val();
		if(txt=="分销商名称/账号"){
			PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱歉输入有误</p>');
			return false;
		}else{
			qQuery.query({
				pid : pid,
				sid : sid,
				dname :txt,
				t :"1",
			});
		}
	},
	del_all : function(){
		var pid = $(".pid").val()
		var sid = $(".sid").val()
		$("#search_text").val("")
		qQuery.query({
			pid : pid,
			sid : sid,
			last:"1",
			qu : "1",
			dname : ""
		});
	}
});



//new Click({container:$("#tab_icon")});
new Main({container:$(".fb_set")});

$(".countBtn").live("click",function(e){
	var action = 0;
	var tarBtn = $(e.currentTarget);
	//var js_a = Number(tarBtn.attr("js"));
	var action = tarBtn.hasClass("add") ? 1 : 0;
	$(".pri_input").each(function(i,ele){
			var price = $(this).val();
			// if(!price){
				// price = js_a;
			// }
			//console.log(price)
			$(this).attr("pa","0");
			var name = $(this).attr("name");
			var sj = $(this).attr("js");
			//console.log(sj);
			var supply = Number($("#pri_d").attr("js"));
			//console.log(supply)
			if(price=='') price = supply;
			if(action==1){
				price = Number(price) + 1.00;
			}else{
				price = Number(price) - 1.00;
				//console.log(typeof(price))
				//console.log(typeof(supply))
				if(price<supply){
					if(sj=="-1"){
						var price = "";
					}else{
						price = sj;
						var price = (price*100)/100;
					}
				//console.log(name+price)
				//PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱  歉结算价不 能小于供货价</p>');
				}
			}
			if(price==""){
				$(this).val("");
			}else{
				price = price.toFixed(2);
				$(this).val(price);
			}
			
			
	});
	var price_d = $("#pri_d").val();
	var supply = Number($("#pri_d").attr("js"));
	if(price_d=='') price_d = supply;
	if(action==1){// plus
		price_d = Number(price_d) + 1.00;
		price_d = price_d.toFixed(2);
		$("#pri_d").val(price_d);
	}else{
		price_d = Number(price_d) - 1.00;
		if(price_d<supply){
			price_d = supply;
			//PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱 歉结算价不能小于供货价</p>');
			price_d = price_d.toFixed(2);
			$("#pri_d").val(price_d);
		}else{
			price_d = price_d.toFixed(2);
			$("#pri_d").val(price_d);
		}
	}
	
})
function pri(){
	var p=$("#pri_d").val();
	var js=Number($("#pri_d").attr("js"));
	if(!p){
		$(".pri_input").each(function(i,ele){
				ele.value="";
				$(this).attr("pa","0")
		});
	}


	if(p<js){
		return false;
	}
	//console.log("num"+PFT_GLOBAL.U.isNum(p)) 
	if(!p || !PFT_GLOBAL.U.isNum(p)){
		//console.log(p)
		$("#pri_d").val(js);
		PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱歉数字输入有误</p>');
		$(".pri_input").each(function(i,ele){
			var price = $(this).attr("js");
			if(price=="-1"){
				ele.value="";
			}else{
				ele.value=price;
			}
		});
		return false;
	}
	else{
		$(".pri_input").each(function(i,e){
			e.value=p;
			$(this).attr("pa","0")
		})
	}
}
$(".countInp").live("blur",function(){
	var p=$("#pri_d").val();
	var js=Number($("#pri_d").attr("js"));
	if(!p){
		return false;
	}
	if(p<js){
		PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱歉结算价不能小于供 货价</p>');
		//return false;
	}
})
$(".formInp").live("blur",function(e){
	var tarBtn = $(e.currentTarget);
	var li = tarBtn.val();  
	var js_sj = Number(tarBtn.attr("js")); 
	var js=Number($("#pri_d").attr("js"));
    if (li >= 10000) {
        //tarBtn.val(9999);
        PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">结算价格必须限制在10000以内</p>');
        return false;
    }
	if(!PFT_GLOBAL.U.isNum(li)){
        
		if(js_sj=="-1"){
			if(!li){
				var js_sj = "";
				tarBtn.val(js_sj);
				tarBtn.attr("pa","1");
				return false;
			}else{
				var js_sj = "";
				tarBtn.val(js_sj);
				tarBtn.attr("pa","1");
				PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱歉数字输 入有误</p>');
				return false;
			}
		}else{
			if(!li){
				var js_sj = "";
				tarBtn.val(js_sj);
				tarBtn.attr("pa","0");
				return false;
			}else{
				tarBtn.val(js_sj);
				tarBtn.attr("pa","1");
				PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱歉数字输入有 误</p>');
				return false;
			}
		}
	}else{
		if(li<js){
			if(js_sj=="-1"){
				tarBtn.val("");
				PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱歉 结算价不能小于供货价</p>');
				return false;
			}
			else{
				// console.log(li+"li")
				// console.log(js+"js")
				tarBtn.val(js_sj);
				PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱歉结 算价不能小于供货价</p>');
				return false;
			}
			
		}else{
			//console.log("ddd")
			tarBtn.attr("pa","0");
			
		}
		// if(li==js_sj){ 
			// //tarBtn.attr("pa","1");
			// tarBtn.attr("pa","1");
		// }else{
			// //tarBtn.attr("pa","0");
			// tarBtn.attr("pa","d");
		// }
	}
})
$("#scope_btn").live("click",function(){
	$(".pri_input").each(function(i,ele){
			var price = $(this).val();
			if(price=="-1"){// plus
				price = "";
			}
			$(this).val(price);
		});
	var postData = $("form#form").serialize();
	var url = "adjusting_dt.php";
	$.ajax({
		type:'POST',url: url,data: postData, dataType:'json',
		//type:'POST',url: 'http://www.12301.cc/admin/pppp.php',data: postData, dataType:'json',
	}).done(function(res) {
		if(res.status=="success"){
			//alert(res.msg);
			PFT_GLOBAL.U.Alert("success",'<p style="width:240px">'+res.msg+'</p>');
			$(".chose_l").text(res.channel);
			$(".pri_input").each(function(i,ele){
				//ele.value="";
				$(this).attr("pa","1")
			});
		}
		else{
			PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">'+res.msg+'</p>');
			//alert(res.msg);
		}
	})			
});
$(document).ready(function(){
       $('#mlistUl tbody:odd').css("backgroundColor","#FAFAFA");

})